package mtvotingserver;

/**
 * Class containing static cleanup method for strings. This is needed because 
 * sometimes telnet clients can send extra characters in the message.
 * 
 * @author Alex Hrin (0807133)
 */
public class StringTools {
    /**
     * Eliminates all characters from a string except for letters, digits and 
     * "_ .,- " Also trims the string.
     * @param input the string that needs to be cleaned up
     * @return the clean string. This method will never return an empty string 
     *         but it can return null.
     */
    public static String cleanString(String input) {
        int startPos = 0;
        for (startPos = input.length() - 1; startPos >= 0; startPos--) {
            char chr = input.charAt(startPos);
            if ( (('0' <= chr) && (chr <= '9')) || 
                 (('A' <= chr) && (chr <= 'Z')) ||
                 (('a' <= chr) && (chr <= 'z')) ||
                  (chr == ' ') || (chr == '_') || (chr == '.') 
                               || (chr == ',') || (chr == '-') )
                break;
            else
                continue;
        }
        
        if (startPos >= 0)
            input = input.substring(0, startPos + 1).trim();
        
        if (input.length() > 0)
            return input;
        else
            return null;
    }
}
